<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 18-12-19
 * Time: 上午9:34
 */

namespace common\components\e_divide;


use common\components\tools\Api;
use common\components\tools\Model;
use common\components\tools\OpensslRSA;
use common\components\tools\Tools;
use common\models\base\EdiviCreditOrderUsers;
use common\models\EdiviCommonBackLogs;
use common\models\EdiviCreditOrder;
use common\models\EdiviCreditOrderUsersPics;
use common\models\EdiviOrderApply;
use common\models\EdiviOrderCard;
use common\models\EdiviOrderClbl;
use common\models\EdiviOrderCompensationAssociates;
use common\models\EdiviOrderDivi;
use common\models\EdiviOrderSendLogs;
use common\models\EdiviOrderYpbl;
use wodrow\yii\rest\ApiException;
use GuzzleHttp\Client;
use yii\base\Component;
use yii\db\Exception;
use yii\helpers\ArrayHelper;

/**
 * 陈皮伟
 * 浙江惠瀜网络科技有限公司
 * 手  机：18768124168
 * 地  址：浙江省杭州市萧山区启迪路198号杭州湾信息港B1座8楼
 * 邮  箱：chenpiwei@hrfax.cn
 *
 * Class EDivide
 * @package common\components\e_divide
 */
class EDivide extends Component
{
    const NAME = 'e分期';

    public $pri_key;
    public $pub_key;

    /**
     * @var EdiviCreditOrder $edivi_c_o
     */
    public $edivi_c_o;
    /**
     * @var EdiviOrderDivi $edivi_divi
     */
    private $edivi_divi;

    public function init()
    {
        parent::init();
        $this->pri_key = Config::PRI_KEY;
        $this->pub_key = Config::PUB_KEY;
    }

    private function transKey()
    {
        $this->pri_key = str_replace('*', '+', $this->pri_key);
        $this->pri_key = str_replace('-', '/', $this->pri_key);
        $this->pri_key = "-----BEGIN PRIVATE KEY-----
{$this->pri_key}
-----END PRIVATE KEY-----";
        $this->pub_key = str_replace('*', '+', $this->pub_key);
        $this->pub_key = str_replace('-', '/', $this->pub_key);
        $this->pub_key = "-----BEGIN PUBLIC KEY-----
{$this->pub_key}
-----END PUBLIC KEY-----";
    }

    /**
     * @param $posts
     * @return mixed|string
     */
    private function encryptAndSign(&$posts)
    {
        $str = json_encode($posts['data']);
        $this->transKey();
        $m = new OpensslRSA($this->pri_key, $this->pub_key);
        $encrypt = $m->PublicEncrypt($str);
        $sign = $m->rsaSign($str);
        $sign = str_replace('+', '*', $sign);
        $sign = str_replace('/', '-', $sign);
        $posts['data'] = $encrypt;
        $posts['sign'] = $sign;
    }

    /**
     * @param $bankCode
     * @return array
     * @throws
     */
    public function getConf($bankCode)
    {
        $econf = Config::$econf;
        $c = $econf[$bankCode];
        if (!$c){
            throw new ApiException(201908191021, "没有找到bankCode配置");
        }
        return $c;
    }

    /**
     * 征信
     * @param $data
     * @return array
     * @throws
     */
    public function credit($data)
    {
        $busiCode = EDivide::B_CODE_CREDIT;
        $orderNo = $data['orderNo'];
        if (!$orderNo){
            throw new ApiException(201902280946, "必须要有订单号");
        }
        $bankCode = $data['bankCode'];
        if (!$bankCode){
            throw new ApiException(201908191022, "必须要有bankCode");
        }
        $conf = $this->getConf($bankCode);
        $edivi_c = EdiviCreditOrder::findOne(['order_no' => $orderNo]);
        if ($edivi_c){
            if ($edivi_c->sign_mode == 2){
                if ($edivi_c->status == EdiviOrderSendLogs::STATUS_CREDIT_SEND){
                    throw new ApiException(201902280947, "该订单号已经提交");
                }
                if ($edivi_c->status == EdiviCommonBackLogs::TRANS_TYPE_4){
                    throw new ApiException(201903281808, "该订单号已经授信通过");
                }
                if ($edivi_c->status == EdiviCommonBackLogs::TRANS_TYPE_3){
                    throw new ApiException(201902280948, "该订单号已经被拒绝:".EdiviCommonBackLogs::getTransTypes()[$edivi_c->status]);
                }
                if ($edivi_c->status == EdiviCommonBackLogs::TRANS_TYPE_9){
                    throw new ApiException(201902280949, "该订单号已经被取消:".EdiviCommonBackLogs::getTransTypes()[$edivi_c->status]);
                }
                if ($edivi_c->status == EdiviCommonBackLogs::TRANS_TYPE_8){
                    throw new ApiException(201902280949, "该订单号已经通过:".EdiviCommonBackLogs::getTransTypes()[$edivi_c->status]);
                }
                if ($edivi_c->status != EdiviCommonBackLogs::TRANS_TYPE_1){
                    throw new ApiException(201902280950, "该订单号退回前不能重复提交");
                }
            }
        }
        $posts = [
            "assurerNo" => $conf['assurerNo'],
            "platNo" => $conf['platNo'],
            "busiCode" => $busiCode,
            "bankType" => $conf['bankType'],
            'data' => [
                'pub' => [
                    "bankCode" => $bankCode,
                    "assurerNo" => $conf['assurerNo'],
                    "platNo" => $conf['platNo'],
                    "busiCode" => $busiCode,
                    'bankType' => $conf['bankType'],
                    "productType" => $conf['productType'],
                    "orderNo" => $orderNo,
                ],
                'req' => [
                    'carType' => $data['carType'], // * 0：新车，1：二手车，2：新能源车
                    'downloadMode' => $data['downloadMode'], // 1-http下载 2-ftp下载 3-sftp下载
                    'intentionPrice' => $data['intentionPrice'], // * 意向车辆价格
                ],
            ],
        ];
        if (!isset($data['users'])) throw new ApiException(201902271710, "没有征信的用户");
        $ed_users = $data['users'];
        $lender_count = 0;
        foreach ($ed_users as $k => $v){
            if ($v['type'] == 'lender'){
                $posts['data']['req']['lender'] = $v;
                $signMode = $v['signMode'];
                unset($posts['data']['req']['lender']['type']);
                unset($posts['data']['req']['lender']['userRelationship']);
                unset($posts['data']['req']['lender']['relationShip']);
                $lender_count ++ ;
            }
            if ($v['type'] == 'spouse'){
                $x0 = $v;
                unset($x0['type']);
                $posts['data']['req']['spouse'][] = $x0;
            }
        }
        if ($lender_count != 1){
            throw new ApiException(201902281657, "主贷人有并且只能有一个");
        }
        $_posts = $posts;
        $this->encryptAndSign($posts);
//        return $_posts;
//        return $posts;
        $json = json_encode($posts, JSON_UNESCAPED_UNICODE);
        $client = new Client([
            'base_uri' => $conf['base_uri'],
        ]);
        $resp = $client->request('POST', '', [
            'body' => $json,
            'headers' => [
                'Content-Type' => 'application/json',
            ],
        ]);
        $_contents = $resp->getBody()->getContents();
        $contents = json_decode($_contents, true);
        if (!isset($contents['code'])) {
            throw new ApiException(201902271513, "edivi征信失败:".$_contents);
        }
        if ($contents['code'] != 0) {
            throw new ApiException(201902271749, $_contents);
        }
        $trans = \Yii::$app->db->beginTransaction();
        try{
            EdiviCreditOrder::deleteAll(['order_no' => $data['orderNo']]);
            $edivi_c = new EdiviCreditOrder();
            $edivi_c->order_no = $data['orderNo'];
            $edivi_c->bank_code = $bankCode;
            $edivi_c->assurer_no = $_posts['assurerNo'];
            $edivi_c->plat_no = $_posts['platNo'];
            $edivi_c->busi_code = $_posts['busiCode'];
            $edivi_c->bank_type = $_posts['bankType'];
            $edivi_c->car_type = $data['carType'];
            $edivi_c->sign_mode = $signMode;
            $edivi_c->download_mode = $data['downloadMode'];
            $edivi_c->intention_price = $data['intentionPrice'];
            if (isset($contents['data']['estageOrderNo']))$edivi_c->estage_order_no = $contents['data']['estageOrderNo'];
            $edivi_c->status = EdiviOrderSendLogs::STATUS_CREDIT_SEND;
            $edivi_c->app_uid = \Yii::$app->chedai_user->id;
            if (!$edivi_c->save()){
                throw new Exception(Model::getModelError($edivi_c));
            }
            $log = new EdiviOrderSendLogs();
            $log->edivi_credit_order_id = $edivi_c->id;
            $log->b_code = $busiCode;
            $log->to_api_data = json_encode($data, JSON_UNESCAPED_UNICODE);
            $log->ed_data = json_encode($_posts, JSON_UNESCAPED_UNICODE);
            $log->to_ed_data = json_encode($posts, JSON_UNESCAPED_UNICODE);
            $log->ed_resp_code = (string)$contents['code'];
            $log->ed_resp_data = json_encode($contents, JSON_UNESCAPED_UNICODE);
            $log->created_at = time();
            if (!$log->save()){
                throw new Exception(Model::getModelError($log));
            }
            $edivi_c_u = new EdiviCreditOrderUsers();
            $edivi_c_u->edivi_credit_order_id = $edivi_c->id;
            foreach ($ed_users as $k => $v){
                $u = clone $edivi_c_u;
                $u->type = $v['type'];
                $u->id_card = $v['idCard'];
                $u->sign_mode = $signMode;
                $u->issue_authority = $v['issueAuthority'];
                $u->phone_num = $v['phoneNum'];
                $u->user_name = $v['userName'];
                $u->start_date = $v['startDate'];
                $u->end_date = $v['endDate'];
                $u->family_address = $v['familyAddress'];
                if (!$u->save()){
                    throw new Exception(Model::getModelError($u));
                }
                $edivi_c_u_p = new EdiviCreditOrderUsersPics();
                $edivi_c_u_p->edivi_credit_order_user_id = $u->id;
                foreach ($v['pics'] as $k1 => $v1){
                    $p = clone $edivi_c_u_p;
                    $p->pic_id = (string)$v1['picId'];
                    $p->pic_code = (string)$v1['picCode'];
                    $p->pic_file_name = $v1['picFileName'];
                    $p->pic_address = $v1['picAddress'];
                    if (!$p->save()){
                        throw new Exception(Model::getModelError($p));
                    }
                }
            }
            $trans->commit();
        }catch (Exception $e){
            $trans->rollBack();
            throw $e;
        }
        $out = [
            'platform_resp' => $contents,
        ];
        return $out;
    }

    /**
     * 电子验签
     * @param $data
     * @return array
     * @throws
     */
    public function creditConfirm($data)
    {
        $busiCode = EDivide::B_CODE_CREDIT_CONFIRM;
        $orderNo = $data['orderNo'];
        if (!$orderNo){
            throw new ApiException(201903051510, "必须要有订单号");
        }
        $edivi_c = EdiviCreditOrder::findOne(['order_no' => $orderNo]);
        if (!$edivi_c){
            throw new ApiException(201908191158, "没有找到订单");
        }
        if ($edivi_c->status != EdiviCommonBackLogs::TRANS_TYPE_19){
            if ($edivi_c->status != EdiviOrderSendLogs::STATUS_CREDIT_SEND){
                throw new ApiException(201903051512, "该订单号无权限发起电子验签(易商金融)");
            }
        }
        if ($edivi_c->sign_mode != 1){
            throw new ApiException(201903051511, "该订单不是电子验签");
        }
        $bankCode = $edivi_c->bank_code;
        $conf = $this->getConf($bankCode);
        $posts = [
            "assurerNo" => $conf['assurerNo'],
            "platNo" => $conf['platNo'],
            "busiCode" => $busiCode,
            "bankType" => $conf['bankType'],
            'data' => [
                'pub' => [
                    "bankCode" => $bankCode,
                    "assurerNo" => $conf['assurerNo'],
                    "platNo" => $conf['platNo'],
                    "busiCode" => $busiCode,
                    'bankType' => $conf['bankType'],
                    "productType" => $conf['productType'],
                    "orderNo" => $orderNo,
                ],
                'req' => [
                    "signConfirm" => 1,
                ],
            ],
        ];
        $_posts = $posts;
        $this->encryptAndSign($posts);
//        return $_posts;
//        return $posts;
        $json = json_encode($posts, JSON_UNESCAPED_UNICODE);
        $client = new Client([
            'base_uri' => $conf['base_uri'],
        ]);
        $resp = $client->request('POST', '', [
            'body' => $json,
            'headers' => [
                'Content-Type' => 'application/json',
            ]
        ]);
        $_contents = $resp->getBody()->getContents();
        $contents = json_decode($_contents, true);
        if (!isset($contents['code'])) {
            throw new ApiException(201903051512, "edivi征信验签失败");
        }
        if ($contents['code'] != 0) {
            throw new ApiException(201903051513, $_contents);
        }
        $trans = \Yii::$app->db->beginTransaction();
        try{
            $log = new EdiviOrderSendLogs();
            $log->edivi_credit_order_id = $edivi_c->id;
            $log->b_code = $busiCode;
            $log->to_api_data = json_encode($data, JSON_UNESCAPED_UNICODE);
            $log->ed_data = json_encode($_posts, JSON_UNESCAPED_UNICODE);
            $log->to_ed_data = json_encode($posts, JSON_UNESCAPED_UNICODE);
            $log->ed_resp_code = (string)$contents['code'];
            $log->ed_resp_data = json_encode($contents, JSON_UNESCAPED_UNICODE);
            $log->created_at = time();
            if (!$log->save()){
                throw new Exception(Model::getModelError($log));
            }
            $trans->commit();
        }catch (Exception $e){
            $trans->rollBack();
            throw $e;
        }
        $out = [
            'platform_resp' => $contents,
        ];
        return $out;
    }

    /**
     * 开卡
     * @param $data
     * @return array
     * @throws
     */
    public function generateCard($data)
    {
        $busiCode = EDivide::B_CODE_CARD_INFO_UP;
        $orderNo = $data['orderNo'];
        if (!$orderNo){
            throw new ApiException(201903011100, "必须要有订单号");
        }
        $edivi_c = EdiviCreditOrder::findOne(['order_no' => $orderNo]);
        if (!$edivi_c){
            throw new ApiException(201903011101, "没有找到订单");
        }
        $bankCode = $edivi_c->bank_code;
        $conf = $this->getConf($bankCode);
        if ($edivi_c->status == EdiviCommonBackLogs::TRANS_TYPE_8){
            throw new ApiException(201902280949, "该订单号已经通过:".EdiviCommonBackLogs::getTransTypes()[$edivi_c->status]);
        }
        if ($edivi_c->status != EdiviCommonBackLogs::TRANS_TYPE_4){
            if ($edivi_c->status != EdiviCommonBackLogs::TRANS_TYPE_2){
                if ($edivi_c->status == EdiviOrderSendLogs::STATUS_GENERATE_CARD_SEND){
                    throw new ApiException(201903011427, "该订单号已经提交开卡");
                }
                if ($edivi_c->status == EdiviOrderSendLogs::STATUS_DIVI_SEND){
                    throw new ApiException(201903011428, "该订单号已经提交分期");
                }
                throw new ApiException(201903011429, "该订单号退回前不能重复提交");
            }
        }
        $posts = [
            "assurerNo" => $conf['assurerNo'],
            "platNo" => $conf['platNo'],
            "busiCode" => $busiCode,
            "bankType" => $conf['bankType'],
            'data' => [
                'pub' => [
                    "bankCode" => $bankCode,
                    "assurerNo" => $conf['assurerNo'],
                    "platNo" => $conf['platNo'],
                    "busiCode" => $busiCode,
                    'bankType' => $conf['bankType'],
                    "productType" => $conf['productType'],
                    "orderNo" => $orderNo,
                ],
                'req' => [
                    'workdate' => $data['workdate'], // * 上传日期YYYY.MM.DD
                    'custsort' => $data['custsort'], // * 证件类型 0-身份证 1-护照 2-军官证 3-士兵证 4-回乡证 5-临时身份证 6-户口本 7-其他 9-警官证
                    'custcode' => $data['custcode'], // * 证件号码<=18
                    'chnsname' => $data['chnsname'], // * 姓名<=30
                    'engname' => $data['engname'], // * 姓名拼音或英文<=50 如：ZHANG,SAN 用英文逗号隔开
                    'sex' => $data['sex'], // * 性别<=4 1-男 2-女 当证件类型为身份证的时候，该字段可以不输。
                    'mrtlstat' => $data['mrtlstat'], // * 婚姻状况<=4 0:未知 1:未婚(无配偶) 2:已婚(有配偶) 3:分居 4:离异 5:丧偶 6:其他。
                    'edulvl' => $data['edulvl'], // * 受教育程度<=4 1:博士及以上 2:硕士研究生 3:大学本科 4:大学专科/电大 5:中专 6:技工学校 7:高中 8:初中 9:小学及以下。
//                    'drawzono' => $data['drawzono'], // 领卡地区号<=5
//                    'drawbrno' => $data['drawbrno'], // 领卡网点号<=5
                    'homestat' => $data['homestat'], // * 住宅状况<=4 1-自有住房 2-分期付款购房 3-租房 4-其他 5-集体宿舍 6-单位分配
                    'hprovince' => $data['hprovince'], // * 住宅地址省份<=5
                    'hcity' => $data['hcity'], // * 住宅地址市<=50
                    'hcounty' => $data['hcounty'], // * 住宅地址县(区)<=50
                    'haddress' => $data['haddress'], // * 住宅地址<=150
                    'homezip' => $data['homezip'], // * 住宅邮编<=6
                    'indate' => $data['indate'], // * 何时入住现址<=10 YYYY-MM-DD
                    'hphonzono' => $data['hphonzono'], // * 住宅电话区号<=5
                    'hphoneno' => $data['hphoneno'], // * 住宅电话号码<=12
                    'mvblno' => $data['mvblno'], // * 手机号码<=11
//                    'email' => $data['email'], // 电子邮箱<=50
                    'unitname' => $data['unitname'], // * 工作单位名称<=60
                    'duty' => $data['duty'], // * 职务<=4 1:国家主席、副主席、总理级副总理、国务委员级 2:部、省级、副部、副省级 3：董事/司、局、地、厅级 4：总经理/县处级 5：科级/部门经理 6：职员/科员级
                    'cophozono' => $data['cophozono'], // * 单位电话区号<=5
                    'cophoneno' => $data['cophoneno'], // * 单位电话号码<=12
                    'caddress' => $data['caddress'], // * 工作单位地址<=120
                    'corpzip' => $data['corpzip'], // * 工作单位地址<=6
                    'joindate' => $data['joindate'], // * 进入现单位工作时间<=10 YYYYMM
                    'yearincome' => $data['yearincome'], // * 本人年收入 Decimal(17,2)元
                    'modelcode' => $data['modelcode'], // * 单位性质<=4 10－国有 20－集体 30－国有控股 40-集体控股 50-三资 60-私营 70-个体 80-外贸 90-股份合作 100-其他股份制 110-民营 120-联营 130-乡镇企业 190-其他
                    'occptn' => $data['occptn'], // * 职业及职级<=4 1公务员2事业单位员工3职员4军人5自由职业者6工人7农民10邮电通讯行业职员11房地产行业职员12交通运输行业职员13法律/司法行业职员14文化/娱乐/体育行业职员15媒介/广告行业职员16科研/教育行业职员17学生18计算机/网络行业职员19商业贸易行业职员20银行/金融/证券/投资行业职员21税务行业职员22咨询行业职员23社会服务行业职员24旅游/饭店行业职员25健康/医疗服务行业职员26管理人员27技术人员28文体明星29无职业30私人业主
                    'carstat' => $data['carstat'], // * 自购车状况<=4 0-有 1-无
                    'reltname1' => $data['reltname1'], // * 联系人1姓名<=30
                    'reltship1' => $data['reltship1'], // * 联系人1与主卡联系人关系<=4 1-父子 2-母子 3-兄弟姐妹 4-亲属 5-夫妻 6-同学 7-同乡 8-朋友 9-同事
                    'reltphzon1' => $data['reltphzon1'], // * 联系人1联系电话区号<=5
                    'relaphone1' => $data['relaphone1'], // * 联系人1联系电话号码<=12
                    'reltmobl1' => $data['reltmobl1'], // * 联系人1手机<=11
                    'cprovince' => $data['cprovince'], // * 单位地址省份<=5
                    'ccity' => $data['ccity'], // * 单位地址市<=30
                    'ccounty' => $data['ccounty'], // * 单位地址县（区）<=30
                    'drawmode' => $data['drawmode'], // * 卡片领取方式<=4 1-自取 2-寄送单位地址 3-寄送住宅地址
                    'statdate' => $data['statdate'], // * 证件有效期<=10 YYYY-MM-DD，如果是长期，则填入9999-12-30
                    'primnat' => $data['primnat'], // * 国籍<=4 见字典
                    'cstsign' => $data['cstsign'], // * 亲见客户签名<=4 1:是 2:否
//                    'almebno' => $data['almebno'], // 联名单位会员号<=20
//                    'outcardno1' => $data['outcardno1'], // 转出卡号/账号<=32
                ],
            ],
        ];
        $_posts = $posts;
        $this->encryptAndSign($posts);
//        return $_posts;
//        return $posts;
        $json = json_encode($posts, JSON_UNESCAPED_UNICODE);
        $client = new Client([
            'base_uri' => $conf['base_uri'],
        ]);
        $resp = $client->request('POST', '', [
            'body' => $json,
            'headers' => [
                'Content-Type' => 'application/json',
            ]
        ]);
        $_contents = $resp->getBody()->getContents();
        $contents = json_decode($_contents, true);
        if (!isset($contents['code'])) {
            throw new ApiException(201903011417, "edivi开卡失败");
        }
        if ($contents['code'] != 0) {
            throw new ApiException(201903011419, $_contents);
        }
        $trans = \Yii::$app->db->beginTransaction();
        try{
            $edivi_c->status = EdiviOrderSendLogs::STATUS_GENERATE_CARD_SEND;
            if (!$edivi_c->save()){
                throw new Exception(Model::getModelError($edivi_c));
            }
            $card = EdiviOrderCard::findOne(['edivi_credit_order_id' => $edivi_c->id]);
            if (!$card){
                $card = new EdiviOrderCard();
                $card->edivi_credit_order_id = $edivi_c->id;
            }
            $card->created_at = time();
            $card->req_data = json_encode($_posts['data']['req'], JSON_UNESCAPED_UNICODE);
            $card->yearincome = $data['yearincome'];
            $card->card_count = 1;
            if (!$card->save()){
                throw new Exception(Model::getModelError($card));
            }
            $log = new EdiviOrderSendLogs();
            $log->edivi_credit_order_id = $edivi_c->id;
            $log->b_code = $busiCode;
            $log->to_api_data = json_encode($data, JSON_UNESCAPED_UNICODE);
            $log->ed_data = json_encode($_posts, JSON_UNESCAPED_UNICODE);
            $log->to_ed_data = json_encode($posts, JSON_UNESCAPED_UNICODE);
            $log->ed_resp_code = (string)$contents['code'];
            $log->ed_resp_data = json_encode($contents, JSON_UNESCAPED_UNICODE);
            $log->created_at = time();
            if (!$log->save()){
                throw new Exception(Model::getModelError($log));
            }
            $trans->commit();
        }catch (Exception $e){
            $trans->rollBack();
            throw $e;
        }
        $out = [
            'platform_resp' => $contents,
        ];
        return $out;
    }

    /**
     * 分期
     * @param $data
     * @return array
     * @throws
     */
    public function divi($data)
    {
        $busiCode = EDivide::B_CODE_DIVI_INFO_UP;
        $orderNo = $data['orderNo'];
        if (!$orderNo){
            throw new ApiException(201903011451, "必须要有订单号");
        }
        $edivi_c = EdiviCreditOrder::findOne(['order_no' => $orderNo]);
        if (!$edivi_c){
            throw new ApiException(201903011452, "没有找到订单");
        }
        $bankCode = $edivi_c->bank_code;
        $conf = $this->getConf($bankCode);
        $this->edivi_c_o = $edivi_c;
        if (!$edivi_c->ediviOrderCard){
            throw new ApiException(201903051040, "没有找到分期卡");
        }
        if ($edivi_c->status == EdiviOrderSendLogs::STATUS_DIVI_SEND){
            throw new ApiException(201903011453, "该订单号已经提交分期");
        }
        if ($edivi_c->status != EdiviOrderSendLogs::STATUS_GENERATE_CARD_SEND){
            throw new ApiException(201903011454, "必须先进行开卡申请");
        }
        $posts = [
            "assurerNo" => $conf['assurerNo'],
            "platNo" => $conf['platNo'],
            "busiCode" => $busiCode,
            "bankType" => $conf['bankType'],
            'data' => [
                'pub' => [
                    "bankCode" => $bankCode,
                    "assurerNo" => $conf['assurerNo'],
                    "platNo" => $conf['platNo'],
                    "busiCode" => $busiCode,
                    'bankType' => $conf['bankType'],
                    "productType" => $conf['productType'],
                    "orderNo" => $orderNo,
                ],
                'req' => [
                    'signMode' => $data['signMode'], // 签约方式 1.电子签约方式 2.影像上传方式
                    // 车辆信息
                    /*'carInfo' => [
                        'carName' => $data['carInfo']['carName'], // * 车型名称<=60 传第一车网车型id
//                        'carPrice' => $data['carInfo']['carPrice'], // 车辆价格 Decimal(17,2)非行内系统字段，不传不影响办理业务
//                        'carType' => $data['carInfo']['carPrice'], // 国产车还是进口车 0-国产车 1-进口车
//                        'frameNo' => $data['carInfo']['frameNo'], // 车架号<=40
//                        'engineNo' => $data['carInfo']['engineNo'], // 发动机号<=30
//                        'demandCarname' => $data['carInfo']['demandCarname'], // 外部车型<=100
                    ],*/
                    /*// 合同信息
                    'contractInfo' => [
                        'field1' => $data['field1'], // 合同扩展字段1
                        'field2' => $data['field2'], // 合同扩展字段2
                        'field3' => $data['field3'], // 合同扩展字段3
                        'field4' => $data['field4'], // 合同扩展字段4
                        'field5' => $data['field5'], // 合同扩展字段5
                        'field6' => $data['field6'], // 合同扩展字段6
                        'field7' => $data['field7'], // 合同扩展字段7
                    ],
                    // 保险信息
                    'insureInfo' => [
                        'isPerformanceInsure' => $data['isPerformanceInsure'], // 是否履约险 1-是,0-否
                        'insuranceCompanyNo' => $data['insuranceCompanyNo'], // 保险公司编号<=100 惠瀜提供
                        'insuranceProdType' => $data['insuranceProdType'], // 保险产品类型 1-低风险车贷险业务购车业务， 2-常态化车贷险业务购车业3-低风险车贷险业务租金贷款， 4-常态化车贷险业务租金贷款
                        'insuranceBusiType' => $data['insuranceBusiType'], // 保险业务类型 1-直租，2-回租，3-空
                        'insuranceOrgNo' => $data['insuranceOrgNo'], // 保险机构编号<=100 惠瀜提供
                    ],*/
                    /*// 分期信息
                    'stageInfo' => [
//                        'businessModel' => $data['stageInfo']['businessModel'], // * 业务模式 1-抵押 -质押 3-保证 4-组合 5-抵押+合作机构保证(先放款后抵押) 6-抵押+合作机构保证(先抵押后放款) 7-阶段性保证+抵押
//                        'repayPeriod' => $data['stageInfo']['repayPeriod'], // * 还款期限 3/6/9/12/18/24/36/48/60
//                        'marketingArchivesNum' => $data['stageInfo']['marketingArchivesNum'], // * 营销档案编号<=8 信用卡卡产品编号
//                        'sfMoney' => $data['stageInfo']['sfMoney'], // * 首付金额 decimal(17,2)
//                        'sfProportion' => $data['sfProportion'], // * 首付比例
//                        'monthincome' => $data['stageInfo']['monthincome'], // * 还款人月均总收入 decimal(17,2)
//                        'property' => $data['stageInfo']['property'], // * 个人总资产
//                        'houseArea' => $data['stageInfo']['houseArea'], // * 现住房面积 单位m²
//                        'othdebtbal' => $data['stageInfo']['othdebtbal'], // * 其他负债余额 decimal(17,2) 不包含本笔
//                        'monrepayamt' => $data['stageInfo']['monrepayamt'], // * 其他月还款额 decimal(17,2) 不包含本笔
//                        'insureamt' => $data['stageInfo']['insureamt'], // * 车损险投保金额 decimal(17,2) 需大于贷款金额合计+首付款金额
//                        'loanMoney' => $data['stageInfo']['loanMoney'], // * 贷款金额合计 decimal(17,2) 包含担保费等，等于最终的放款金额
                    ],*/
                    // 担保信息(里面信息可以为空，但是此节点须有。里面信息根据贵方所选业务模式来传相应字段，其中抵押和质押为互斥，有抵押不传质押信息、有质押不传抵押信息)
                    'guaranteeInfo' => null,
                    /*// 附加费开卡信息
                    'financeChargeCardInfo' => [],*/
                    // 打款信息
                    /*'payMentInfo' => [
                        'payMode' => $data['payMentInfo']['payMode'], // * 支付模式 1-受托支付;2-实体POS刷卡分期
                        'payeeType' => $data['payMentInfo']['payeeType'], // * 收款对象类型 1合作机构;2合作单位;3合作机构+合作单位
                        'agencyId' => Config::$agencyId, // * 第三方机构编码<=15
                        'recommendAgencyCode' => $data['payMentInfo']['recommendAgencyCode'], // * 推荐机构编码<=100
                        'agencyReceiveAmt1' => $data['payMentInfo']['agencyReceiveAmt1'], // * 第三方收款金额1 decimal(17,2)
                        'agencyReceiveType1' => $data['payMentInfo']['agencyReceiveType1'], // * 第三方机构收款金额1标志 1:贷款金额，2:附加费
                        'agencyReceiveAmt2' => $data['payMentInfo']['agencyReceiveAmt2'], // * 第三方机构收款金额2 decimal(17,2)
                        'agencyReceiveType2' => $data['payMentInfo']['agencyReceiveType2'], // * 第三方机构收款金额2标志 1:贷款金额，2:附加费
                        // 合作单位信息（数组）
                        'cocomNos' => [],
                    ],*/
                    /*// (若没有值则不传该节点，例如没有GTHKRCL，则无GTHKRCL节点，不要传null等)
                    // 主借款人材料
                    'JKRCL' => [
                        'name' => $data['JKRCL']['name'], // * 姓名<=30
                        'idCard' => $data['JKRCL']['idCard'], // * 身份<=18
                        'mobile' => $data['JKRCL']['mobile'], // * 手机号码<=11
                        'materials' => [
                            [
                                'materialCode' => $data['JKRCL']['materials'][]['materialCode'], // * 材料编码<=30 见字典
                                'materialsType' => $data['JKRCL']['materials'][]['materialsType'], // * 材料类型<=2 0:图片 1:视频
                                'downloadMode' => $data['JKRCL']['materials'][]['downloadMode'], // * 材料下载方式<=2 1-http下载 2-ftp下载 -sftp下载
                                'mates' => [
                                    [
                                        'demandMateId' => $data['JKRCL']['materials'][]['mates'][]['demandMateId'], // * 材料ID 材料唯一标识
                                        'materialsPic' => $data['JKRCL']['materials'][]['mates'][]['materialsPic'], // * 材料下载地址 图片大小须小于500K，单个视频时长最大10分钟；如果ftp下载，请提供文件路径
                                        'fileName' => $data['JKRCL']['materials'][]['mates'][]['fileName'], // * 材料文件名 这个字段需要以文件后缀名fileName中的.jpg)
                                    ],
                                ],
                            ],
                        ],
                    ],
                    // 共同还款人材料
                    'GTHKRCL' => [],
                    // 担保人材料
                    'DBRCL' => [],*/
                ],
            ],
        ];
        // 车辆信息
        if (isset($data['carInfo']) && $data['carInfo']){
            $posts['data']['req']['carInfo'] = $data['carInfo'];
        }
        // 打款信息
        if (isset($data['payMentInfo']) && $data['payMentInfo']){
            $posts['data']['req']['payMentInfo'] = $data['payMentInfo'];
        }
        // 主借款人材料
        if (isset($data['JKRCL']) && $data['JKRCL']){
            $posts['data']['req']['JKRCL'] = $data['JKRCL'];
        }
        // 共同还款人材料
        if (isset($data['GTHKRCL']) && $data['GTHKRCL']){
            $posts['data']['req']['GTHKRCL'] = $data['GTHKRCL'];
        }
        // 担保人材料
        if (isset($data['DBRCL']) && $data['DBRCL']){
            $posts['data']['req']['DBRCL'] = $data['DBRCL'];
        }
        // 担保信息
        if (isset($data['guaranteeInfo']) && $data['guaranteeInfo']){
            $posts['data']['req']['guaranteeInfo'] = $data['guaranteeInfo'];
        }
        // 合同信息
        if (isset($data['contractInfo']) && $data['contractInfo']){
            $posts['data']['req']['contractInfo'] = $data['contractInfo'];
        }
        // 分期信息
        if (isset($data['stageInfo']) && $data['stageInfo']){
            $posts['data']['req']['stageInfo'] = $data['stageInfo'];
        }
        // 保险信息
        if (isset($data['insureInfo']) && $data['insureInfo']){
            $posts['data']['req']['insureInfo'] = $data['insureInfo'];
        }
        // 开卡
        if (isset($data['financeChargeCardInfo']) && $data['financeChargeCardInfo']){
            $posts['data']['req']['financeChargeCardInfo'] = $data['financeChargeCardInfo'];
        }
        $divi = EdiviOrderDivi::findOne(['edivi_credit_order_id' => $edivi_c->id]);
        if (!$divi){
            $divi = new EdiviOrderDivi();
            $divi->edivi_credit_order_id = $edivi_c->id;
        }
        $divi->created_at = time();
        $divi->req_data = json_encode($posts['data']['req'], JSON_UNESCAPED_UNICODE);
        $divi->loan_money = $data['stageInfo']['loanMoney'];
        $divi->sf_money = $data['stageInfo']['sfMoney'];
        if (isset($data['stageInfo']['insureamt']))$divi->insureamt = $data['stageInfo']['insureamt'];
        $divi->finance_charge = 0;
        if (isset($data['financeChargeCardInfo']['financeCharge'])){
            $divi->finance_charge = $data['financeChargeCardInfo']['financeCharge'];
        }else{
//            if (isset($data['payMentInfo']['cocomNos'])){
//                foreach ($data['payMentInfo']['cocomNos'] as $k => $v){
//                    if ($v['cocomReceiveType'] == 2){
//                        $divi->finance_charge = $v['cocomReceiveAmt'];
//                    }
//                }
//            }
        }
        $divi->monthincome = $data['stageInfo']['monthincome'];
        $this->edivi_divi = $divi;
        $this->sfControl();
        $this->fjfControl();
        $this->cardCountCheck($data);
        $this->dkcsControl();
        $this->dylCheck($data);
        $this->dkjehjControl($data);
        $this->csxtbjeControl();
        $_posts = $posts;
        $this->encryptAndSign($posts);
//        return $_posts;
//        return $posts;
        $json = json_encode($posts, JSON_UNESCAPED_UNICODE);
        $client = new Client([
            'base_uri' => $conf['base_uri'],
        ]);
        $resp = $client->request('POST', '', [
            'body' => $json,
            'headers' => [
                'Content-Type' => 'application/json',
            ]
        ]);
        $_contents = $resp->getBody()->getContents();
        $contents = json_decode($_contents, true);
        if (!isset($contents['code'])) {
            throw new ApiException(201903011417, "edivi分期失败");
        }
        if ($contents['code'] != 0) {
            if ($contents['code'] == 8011){
                $resp = Api::request('/e_divide/default/order-info', ['orderno' => $orderNo]);
                $_j = json_encode($resp->content, true);
                if (isset($_j['data']['platform_resp']['data']['remark'])){
                    if ($_j['data']['platform_resp']['data']['remark'] != '贷款审批中'){
                        throw new ApiException(201905301025, $resp->content);
                    }
                }
            }else{
                throw new ApiException(201903011419, $_contents);
            }
        }
        $trans = \Yii::$app->db->beginTransaction();
        try{
            $edivi_c->status = EdiviOrderSendLogs::STATUS_DIVI_SEND;
            if (!$edivi_c->save()){
                throw new Exception(Model::getModelError($edivi_c));
            }
            $divi = $this->edivi_divi;
            if (!$divi->save()){
                throw new Exception(Model::getModelError($divi));
            }
            $log = new EdiviOrderSendLogs();
            $log->edivi_credit_order_id = $edivi_c->id;
            $log->b_code = $busiCode;
            $log->to_api_data = json_encode($data, JSON_UNESCAPED_UNICODE);
            $log->ed_data = json_encode($_posts, JSON_UNESCAPED_UNICODE);
            $log->to_ed_data = json_encode($posts, JSON_UNESCAPED_UNICODE);
            $log->ed_resp_code = (string)$contents['code'];
            $log->ed_resp_data = json_encode($contents, JSON_UNESCAPED_UNICODE);
            $log->created_at = time();
            if (!$log->save()){
                throw new Exception(Model::getModelError($log));
            }
            $trans->commit();
        }catch (Exception $e){
            $trans->rollBack();
            throw $e;
        }
        $out = [
            'platform_resp' => $contents,
        ];
        return $out;
    }

    /**
     * 押品补录
     * @return array
     * @throws
     */
    public function ypbl($data)
    {
        $busiCode = EDivide::B_CODE_SUPPLEMENT_OF_MORTGAGE_INFORMATION;
        $orderNo = $data['orderNo'];
        if (!$orderNo){
            throw new ApiException(201903041121, "必须要有订单号");
        }
        $edivi_c = EdiviCreditOrder::findOne(['order_no' => $orderNo]);
        if (!$edivi_c){
            throw new ApiException(201903041122, "没有找到订单");
        }
        $bankCode = $edivi_c->bank_code;
        $conf = $this->getConf($bankCode);
        if ($edivi_c->status != EdiviCommonBackLogs::TRANS_TYPE_5){
            if ($edivi_c->status == EdiviOrderSendLogs::STATUS_YPBU_SEND){
                throw new ApiException(201908141710, "已经在押品补录中");
            }
            throw new ApiException(201903041123, "订单状态必须为:".EdiviCommonBackLogs::getTransTypes()[EdiviCommonBackLogs::TRANS_TYPE_5]);
        }
        $posts = [
            "assurerNo" => $conf['assurerNo'],
            "platNo" => $conf['platNo'],
            "busiCode" => $busiCode,
            "bankType" => $conf['bankType'],
            'data' => [
                'pub' => [
                    "bankCode" => $bankCode,
                    "assurerNo" => $conf['assurerNo'],
                    "platNo" => $conf['platNo'],
                    "busiCode" => $busiCode,
                    'bankType' => $conf['bankType'],
                    "productType" => $conf['productType'],
                    "orderNo" => $orderNo,
                ],
                'req' => [
//                    "taskId" => $data['taskId'], // 任务号；String(18)；任务号；必传
                    /*// (材料信息)
                    "materials" => [
                        "materialCode" => "", // 材料编码；String(30)；材料编码，见字典；必传
                        "materialsType" => "", // 材料类型；String(2)；0-图片 1-视频；必传
                        "downloadMode" => "", // 材料下载方式；String(2)；1-http下载，2-ftp下载，3-sftp下载；必传
                        "mates" => [
                            [
                                "demandMateId" => "", // 材料ID；Int(11)；材料唯一标识；必传
                                "materialsPic" => "", // 材料下载地址；String(250)；图片大小须小于500K，单个视频时长最大10分钟；如果ftp下载，请提供文件路径；必传
                                "fileName" => "", // 材料文件名；String(30)；材料文件名；必传
                            ],
                        ],
                    ],*/
                ],
            ],
        ];
        $posts['data']['req']['materials'] = $data['materials'];
        $posts['data']['req']['guaranteeInfo'] = $data['guaranteeInfo'];
        $_posts = $posts;
        $this->encryptAndSign($posts);
//        return $_posts;
//        return $posts;
        $json = json_encode($posts, JSON_UNESCAPED_UNICODE);
        $client = new Client([
            'base_uri' => $conf['base_uri'],
        ]);
        $resp = $client->request('POST', '', [
            'body' => $json,
            'headers' => [
                'Content-Type' => 'application/json',
            ]
        ]);
        $_contents = $resp->getBody()->getContents();
        $contents = json_decode($_contents, true);
        if (!isset($contents['code'])) {
            throw new ApiException(201903041143, "押品补录失败");
        }
        if ($contents['code'] != 0) {
            throw new ApiException(201903041144, $_contents);
        }
        $trans = \Yii::$app->db->beginTransaction();
        try{
            $edivi_c->status = EdiviOrderSendLogs::STATUS_YPBU_SEND;
            if (!$edivi_c->save()){
                throw new Exception(Model::getModelError($edivi_c));
            }
            $edivi_ypbu = new EdiviOrderYpbl();
            $pics = new EdiviCreditOrderUsersPics();
            $edivi_ypbu->edivi_credit_order_id = $edivi_c->id;
            $edivi_ypbu->created_at = time();
            foreach ($data['materials'] as $k => $v){
                $ylbu = clone $edivi_ypbu;
                $ylbu->material_code = $v['materialCode'];
                $ylbu->materials_type = $v['materialsType'];
                $ylbu->download_mode = $v['downloadMode'];
                if (!$ylbu->save()){
                    throw new Exception(Model::getModelError($ylbu));
                }
                $pics->edivi_yp_id = $ylbu->id;
                foreach ($v['mates'] as $k1 => $v1) {
                    $p = clone $pics;
                    $p->pic_id = (string)$v1['demandMateId'];
                    $p->pic_code = $ylbu->material_code;
                    $p->pic_file_name = $v1['fileName'];
                    $p->pic_address = $v1['materialsPic'];
                    if (!$p->save()){
                        throw new Exception(Model::getModelError($p));
                    }
                }
            }
            $log = new EdiviOrderSendLogs();
            $log->edivi_credit_order_id = $edivi_c->id;
            $log->b_code = $busiCode;
            $log->to_api_data = json_encode($data, JSON_UNESCAPED_UNICODE);
            $log->ed_data = json_encode($_posts, JSON_UNESCAPED_UNICODE);
            $log->to_ed_data = json_encode($posts, JSON_UNESCAPED_UNICODE);
            $log->ed_resp_code = (string)$contents['code'];
            $log->ed_resp_data = json_encode($contents, JSON_UNESCAPED_UNICODE);
            $log->created_at = time();
            if (!$log->save()){
                throw new Exception(Model::getModelError($log));
            }
            $trans->commit();
        }catch (Exception $e){
            $trans->rollBack();
            throw $e;
        }
        $out = [
            'platform_resp' => $contents,
        ];
        return $out;
    }

    /**
     * 材料补录
     * @param array $data
     * @return array
     * @throws
     */
    public function clbl($data)
    {
        $busiCode = EDivide::B_CODE_SUPPLEMENTARY_STAGING_MATERIALS;
        $orderNo = $data['orderNo'];
        if (!$orderNo){
            throw new ApiException(201903041524, "必须要有订单号");
        }
        $edivi_c = EdiviCreditOrder::findOne(['order_no' => $orderNo]);
        if (!$edivi_c){
            throw new ApiException(201903041522, "没有找到订单");
        }
        $bankCode = $edivi_c->bank_code;
        $conf = $this->getConf($bankCode);
        if ($edivi_c->status != EdiviCommonBackLogs::TRANS_TYPE_6){
            if ($edivi_c->status == EdiviOrderSendLogs::STATUS_CLBU_SEND){
                throw new ApiException(201908141710, "已经在材料补录中");
            }
            throw new ApiException(201903041523, "订单状态必须为:".EdiviCommonBackLogs::getTransTypes()[EdiviCommonBackLogs::TRANS_TYPE_6]);
        }
        $posts = [
            "assurerNo" => $conf['assurerNo'],
            "platNo" => $conf['platNo'],
            "busiCode" => $busiCode,
            "bankType" => $conf['bankType'],
            'data' => [
                'pub' => [
                    "bankCode" => $bankCode,
                    "assurerNo" => $conf['assurerNo'],
                    "platNo" => $conf['platNo'],
                    "busiCode" => $busiCode,
                    'bankType' => $conf['bankType'],
                    "productType" => $conf['productType'],
                    "orderNo" => $orderNo,
                ],
                'req' => [
                    "taskId" => $edivi_c->bl_task_id, // 任务号；String(18)；任务号；必传
                    /*// (材料信息)
                    "materials" => [
                        "materialCode" => "", // 材料编码；String(30)；材料编码，见字典；必传
                        "materialsType" => "", // 材料类型；String(2)；0-图片 1-视频；必传
                        "downloadMode" => "", // 材料下载方式；String(2)；1-http下载，2-ftp下载，3-sftp下载；必传
                        "mates" => [
                            [
                                "demandMateId" => "", // 材料ID；Int(11)；材料唯一标识；必传
                                "materialsPic" => "", // 材料下载地址；String(250)；图片大小须小于500K，单个视频时长最大10分钟；如果ftp下载，请提供文件路径；必传
                                "fileName" => "", // 材料文件名；String(30)；材料文件名；必传
                            ],
                        ],
                    ],*/
                    /*// (材料信息)
                    "materials" => [
                        "materialCode" => "", // 材料编码；String(30)；材料编码，见字典；必传
                        "materialsType" => "", // 材料类型；String(2)；0-图片 1-视频；必传
                        "downloadMode" => "", // 材料下载方式；String(2)；1-http下载，2-ftp下载，3-sftp下载；必传
                        "mates" => [
                            [
                                "demandMateId" => "", // 材料ID；Int(11)；材料唯一标识；必传
                                "materialsPic" => "", // 材料下载地址；String(250)；图片大小须小于500K，单个视频时长最大10分钟；如果ftp下载，请提供文件路径；必传
                                "fileName" => "", // 材料文件名；String(30)；材料文件名；必传
                            ],
                        ],
                    ],*/
                ],
            ],
        ];
        $posts['data']['req']['materials'] = $data['materials'];
        $_posts = $posts;
        $this->encryptAndSign($posts);
//        return $_posts;
//        return $posts;
        $json = json_encode($posts, JSON_UNESCAPED_UNICODE);
        $client = new Client([
            'base_uri' => $conf['base_uri'],
        ]);
        $resp = $client->request('POST', '', [
            'body' => $json,
            'headers' => [
                'Content-Type' => 'application/json',
            ]
        ]);
        $_contents = $resp->getBody()->getContents();
        $contents = json_decode($_contents, true);
        if (!isset($contents['code'])) {
            throw new ApiException(201903041143, "押品补录失败");
        }
        if ($contents['code'] != 0) {
            throw new ApiException(201903041144, $_contents);
        }
        $trans = \Yii::$app->db->beginTransaction();
        try{
            $edivi_c->status = EdiviOrderSendLogs::STATUS_CLBU_SEND;
            $task_id = $edivi_c->bl_task_id;
            $edivi_c->bl_task_id = null;
            $edivi_c->bl_task_type = null;
            if (!$edivi_c->save()){
                throw new Exception(Model::getModelError($edivi_c));
            }
            $edivi_clbu = new EdiviOrderClbl();
            $pics = new EdiviCreditOrderUsersPics();
            $edivi_clbu->edivi_credit_order_id = $edivi_c->id;
            $edivi_clbu->created_at = time();
            $edivi_clbu->bl_task_id = $task_id;
            foreach ($data['materials'] as $k => $v){
                $clbu = clone $edivi_clbu;
                $clbu->material_code = $v['materialCode'];
                $clbu->materials_type = $v['materialsType'];
                $clbu->download_mode = $v['downloadMode'];
                if (!$clbu->save()){
                    throw new Exception(Model::getModelError($clbu));
                }
                $pics->edivi_cl_id = $clbu->id;
                foreach ($v['mates'] as $k1 => $v1) {
                    $p = clone $pics;
                    $p->pic_id = (string)$v1['demandMateId'];
                    $p->pic_code = $clbu->material_code;
                    $p->pic_file_name = $v1['fileName'];
                    $p->pic_address = $v1['materialsPic'];
                    if (!$p->save()){
                        throw new Exception(Model::getModelError($p));
                    }
                }
            }
            $log = new EdiviOrderSendLogs();
            $log->edivi_credit_order_id = $edivi_c->id;
            $log->b_code = $busiCode;
            $log->to_api_data = json_encode($data, JSON_UNESCAPED_UNICODE);
            $log->ed_data = json_encode($_posts, JSON_UNESCAPED_UNICODE);
            $log->to_ed_data = json_encode($posts, JSON_UNESCAPED_UNICODE);
            $log->ed_resp_code = (string)$contents['code'];
            $log->ed_resp_data = json_encode($contents, JSON_UNESCAPED_UNICODE);
            $log->created_at = time();
            if (!$log->save()){
                throw new Exception(Model::getModelError($log));
            }
            $trans->commit();
        }catch (Exception $e){
            $trans->rollBack();
            throw $e;
        }
        $out = [
            'platform_resp' => $contents,
        ];
        return $out;
    }

    /**
     * 3.9.1.1 订单状态查询
     * @param $order_no
     * @return array
     * @throws ApiException
     * @throws Exception
     * @throws \GuzzleHttp\Exception\GuzzleException
     * @throws \yii\base\Exception
     */
    public function orderInfo($order_no)
    {
        $busiCode = EDivide::B_CODE_ORDER_INFO;
        $orderNo = $order_no;
        if (!$orderNo){
            throw new ApiException(201903041524, "必须要有订单号");
        }
        $edivi_c = EdiviCreditOrder::findOne(['order_no' => $orderNo]);
        if (!$edivi_c){
            throw new ApiException(201903041522, "没有找到订单");
        }
        $bankCode = $edivi_c->bank_code;
        $conf = $this->getConf($bankCode);
        $posts = [
            "assurerNo" => $conf['assurerNo'],
            "platNo" => $conf['platNo'],
            "busiCode" => $busiCode,
            "bankType" => $conf['bankType'],
            'data' => [
                'pub' => [
                    "bankCode" => $bankCode,
                    "assurerNo" => $conf['assurerNo'],
                    "platNo" => $conf['platNo'],
                    "busiCode" => $busiCode,
                    'bankType' => $conf['bankType'],
                    "productType" => $conf['productType'],
                    "orderNo" => $orderNo,
                ],
            ],
        ];
        $_posts = $posts;
        $this->encryptAndSign($posts);
//        return $_posts;
//        return $posts;
        $json = json_encode($posts, JSON_UNESCAPED_UNICODE);
        $client = new Client([
            'base_uri' => $conf['base_uri'],
        ]);
        $resp = $client->request('POST', '', [
            'body' => $json,
            'headers' => [
                'Content-Type' => 'application/json',
            ]
        ]);
        $_contents = $resp->getBody()->getContents();
        $contents = json_decode($_contents, true);
        $out = [
            'platform_resp' => $contents,
        ];
        return $out;
    }

    /**
     * 3.10 订单申请
     * @param $data
     * @return array
     * @throws
     */
    public function orderApply($data)
    {
        $busiCode = EDivide::B_CODE_ORDER_APPLY;
        $orderNo = $data['orderNo'];
        if (!$orderNo){
            throw new ApiException(201904281151, "必须要有订单号");
        }
        $edivi_c = EdiviCreditOrder::findOne(['order_no' => $orderNo]);
        if (!$edivi_c){
            throw new ApiException(201904281152, "没有找到订单");
        }
        $bankCode = $edivi_c->bank_code;
        $conf = $this->getConf($bankCode);
        $t = date("Y-m-d H:i:s");
        $posts = [
            "assurerNo" => $conf['assurerNo'],
            "platNo" => $conf['platNo'],
            "busiCode" => $busiCode,
            "bankType" => $conf['bankType'],
            'data' => [
                'pub' => [
                    "bankCode" => $bankCode,
                    "assurerNo" => $conf['assurerNo'],
                    "platNo" => $conf['platNo'],
                    "busiCode" => $busiCode,
                    'bankType' => $conf['bankType'],
                    "productType" => $conf['productType'],
                    "orderNo" => $orderNo,
                ],
                'req' => [
                    "applyType" => $data['applyType'], // 申请类型   2-取消订单申请  3-撤销取消订单申请
                    "applyReason" => $data['applyReason'], // 申请原因
                    "applyTime" => $t, // 申请时间
                    "applyName" => $data['applyName'], // 申请人姓名
                ],
            ],
        ];
        $_posts = $posts;
        $this->encryptAndSign($posts);
        $json = json_encode($posts, JSON_UNESCAPED_UNICODE);
        $client = new Client([
            'base_uri' => $conf['base_uri'],
        ]);
        $resp = $client->request('POST', '', [
            'body' => $json,
            'headers' => [
                'Content-Type' => 'application/json',
            ]
        ]);
        $_contents = $resp->getBody()->getContents();
        $contents = json_decode($_contents, true);
        if (!isset($contents['code'])) {
            throw new ApiException(201904281315, "申请失败");
        }
        if ($contents['code'] != 0) {
            throw new ApiException(201904281314, $_contents);
        }
        $trans = \Yii::$app->db->beginTransaction();
        try{
            $edivi_oa = new EdiviOrderApply();
            $edivi_oa->edivi_order_id = $edivi_c->id;
            $edivi_oa->created_at = time();
            $edivi_oa->req_data = json_encode($_posts['data']['req'], JSON_UNESCAPED_UNICODE);
            $edivi_oa->resp = $_contents;
            $edivi_oa->resp_code = (string)$contents['code'];
            $edivi_oa->resp_data_code = (string)$contents['data']['code'];
            if (!$edivi_oa->save()){
                throw new Exception(Model::getModelError($edivi_oa));
            }
            if ($edivi_oa->resp_data_code == '3'){
                $edivi_c->status = EdiviCommonBackLogs::TRANS_TYPE_1;
                if (!$edivi_c->save()){
                    throw new Exception(Model::getModelError($edivi_c));
                }
            }else{
                $edivi_c->status = EdiviOrderSendLogs::STATUS_ORDER_APPLY;
                if (!$edivi_c->save()){
                    throw new Exception(Model::getModelError($edivi_c));
                }
            }
            $log = new EdiviOrderSendLogs();
            $log->edivi_credit_order_id = $edivi_c->id;
            $log->b_code = $busiCode;
            $log->to_api_data = json_encode($data, JSON_UNESCAPED_UNICODE);
            $log->ed_data = json_encode($_posts, JSON_UNESCAPED_UNICODE);
            $log->to_ed_data = json_encode($posts, JSON_UNESCAPED_UNICODE);
            $log->ed_resp_code = (string)$contents['code'];
            $log->ed_resp_data = json_encode($contents, JSON_UNESCAPED_UNICODE);
            $log->created_at = time();
            if (!$log->save()){
                throw new Exception(Model::getModelError($log));
            }
            $trans->commit();
        }catch (Exception $e){
            $trans->rollBack();
            throw $e;
        }
        $out = [
            'platform_resp' => $contents,
        ];
        return $out;
    }

    /**
     * 3.10 订单通知记录查询
     * @param $data
     * @return array
     * @throws
     */
    public function checkOrderNoticeLog($data)
    {
        $busiCode = EDivide::B_CODE_LAST_NOTIFY_CHECK;
        $orderNo = $data['orderNo'];
        if (!$orderNo){
            throw new ApiException(201907091514, "必须要有订单号");
        }
        $edivi_c = EdiviCreditOrder::findOne(['order_no' => $orderNo]);
        if (!$edivi_c){
            throw new ApiException(201907091515, "没有找到订单");
        }
        $bankCode = $edivi_c->bank_code;
        $conf = $this->getConf($bankCode);
        $t = date("Y-m-d H:i:s");
        $posts = [
            "assurerNo" => $conf['assurerNo'],
            "platNo" => $conf['platNo'],
            "busiCode" => $busiCode,
            "bankType" => $conf['bankType'],
            'data' => [
                'pub' => [
                    "bankCode" => $bankCode,
                    "assurerNo" => $conf['assurerNo'],
                    "platNo" => $conf['platNo'],
                    "busiCode" => $busiCode,
                    'bankType' => $conf['bankType'],
                    "productType" => $conf['productType'],
                    "orderNo" => $orderNo,
                ],
            ],
        ];
        $_posts = $posts;
        $this->encryptAndSign($posts);
        $json = json_encode($posts, JSON_UNESCAPED_UNICODE);
        $client = new Client([
            'base_uri' => $conf['base_uri'],
        ]);
        $resp = $client->request('POST', '', [
            'body' => $json,
            'headers' => [
                'Content-Type' => 'application/json',
            ]
        ]);
        $_contents = $resp->getBody()->getContents();
        $contents = json_decode($_contents, true);
        if (!isset($contents['code'])) {
            throw new ApiException(201907091516, "订单通知记录查询失败");
        }
        if ($contents['code'] != 0) {
            throw new ApiException(201907091517, $_contents);
        }
        $trans = \Yii::$app->db->beginTransaction();
        try{
            $log = new EdiviOrderSendLogs();
            $log->edivi_credit_order_id = $edivi_c->id;
            $log->b_code = $busiCode;
            $log->to_api_data = json_encode($data, JSON_UNESCAPED_UNICODE);
            $log->ed_data = json_encode($_posts, JSON_UNESCAPED_UNICODE);
            $log->to_ed_data = json_encode($posts, JSON_UNESCAPED_UNICODE);
            $log->ed_resp_code = (string)$contents['code'];
            $log->ed_resp_data = json_encode($contents, JSON_UNESCAPED_UNICODE);
            $log->created_at = time();
            if (!$log->save()){
                throw new Exception(Model::getModelError($log));
            }
            $trans->commit();
        }catch (Exception $e){
            $trans->rollBack();
            throw $e;
        }
        $out = [
            'platform_resp' => $contents,
        ];
        return $out;
    }

    /**
     * 3.12 补查关联人
     * @param $data
     * @return array
     * @throws
     */
    public function compensationAssociates($data)
    {
        $busiCode = EDivide::B_CODE_COMPENSATION_ASSOCIATES;
        $orderNo = $data['orderNo'];
        if (!$orderNo){
            throw new ApiException(201907091319, "必须要有订单号");
        }
        $edivi_c = EdiviCreditOrder::findOne(['order_no' => $orderNo]);
        if (!$edivi_c){
            throw new ApiException(201907091320, "没有找到订单");
        }
        $bankCode = $edivi_c->bank_code;
        $conf = $this->getConf($bankCode);
        $t = date("Y-m-d H:i:s");
        $posts = [
            "assurerNo" => $conf['assurerNo'],
            "platNo" => $conf['platNo'],
            "busiCode" => $busiCode,
            "bankType" => $conf['bankType'],
            'data' => [
                'pub' => [
                    "bankCode" => $bankCode,
                    "assurerNo" => $conf['assurerNo'],
                    "platNo" => $conf['platNo'],
                    "busiCode" => $busiCode,
                    'bankType' => $conf['bankType'],
                    "productType" => $conf['productType'],
                    "orderNo" => $orderNo,
                ],
                'req' => [
                    'downloadMode' => 1, // 材料下载方式 1-http下载
                    'spouse' => $data['spouse'],
                ],
            ],
        ];
        $_posts = $posts;
        $this->encryptAndSign($posts);
        $json = json_encode($posts, JSON_UNESCAPED_UNICODE);
        $client = new Client([
            'base_uri' => $conf['base_uri'],
        ]);
        $resp = $client->request('POST', '', [
            'body' => $json,
            'headers' => [
                'Content-Type' => 'application/json',
            ]
        ]);
        $_contents = $resp->getBody()->getContents();
        $contents = json_decode($_contents, true);
        if (!isset($contents['code'])) {
            throw new ApiException(201907091323, "补查关联人失败");
        }
        if ($contents['code'] != 0) {
            throw new ApiException(201907091324, $_contents);
        }
        $trans = \Yii::$app->db->beginTransaction();
        try{
//            $edivi_c->status = EdiviOrderSendLogs::STATUS_COMPENSATION_ASSOCIATES;
//            if (!$edivi_c->save()){
//                throw new Exception(Model::getModelError($edivi_c));
//            }
            $edivi_ca = new EdiviOrderCompensationAssociates();
            $edivi_ca->edivi_credit_order_id = $edivi_c->id;
            $edivi_ca->spouse_data = json_encode($data['spouse'], JSON_UNESCAPED_UNICODE);
            $edivi_ca->id_card = $data['spouse']['idCard'];
            $edivi_ca->issue_authority = $data['spouse']['issueAuthority'];
            $edivi_ca->phone_num = $data['spouse']['phoneNum'];
            $edivi_ca->user_name = $data['spouse']['userName'];
            $edivi_ca->start_date = $data['spouse']['startDate'];
            $edivi_ca->end_date = $data['spouse']['endDate'];
            if (isset($data['spouse']['familyAddress']))$edivi_ca->family_address = $data['spouse']['familyAddress'];
            if (!$edivi_ca->save()){
                throw new Exception(Model::getModelError($edivi_ca));
            }
            if (EdiviCreditOrderUsers::findOne(['edivi_credit_order_id' => $edivi_c->id, 'id_card' => $edivi_ca->id_card])){
                throw new ApiException(201908211117, "该客户此订单已存在");
            }
            $u = new \common\models\EdiviCreditOrderUsers();
            $u->edivi_credit_order_id = $edivi_c->id;
            $u->type = 'spouse';
            $u->is_ca = 1;
            $u->id_card = $edivi_ca->id_card;
            $u->sign_mode = $data['spouse']['signMode'];
            $u->issue_authority = $edivi_ca->issue_authority;
            $u->phone_num = $edivi_ca->phone_num;
            $u->user_name = $edivi_ca->user_name;
            $u->start_date = $edivi_ca->start_date;
            $u->end_date = $edivi_ca->end_date;
            $u->family_address = $edivi_ca->family_address;
            if (!$u->save()){
                throw new Exception(Model::getModelError($u));
            }
            $edivi_c_u_p = new EdiviCreditOrderUsersPics();
            $edivi_c_u_p->edivi_credit_order_user_id = $u->id;
            foreach ($data['spouse']['pics'] as $k1 => $v1){
                $p = clone $edivi_c_u_p;
                $p->pic_id = (string)$v1['picId'];
                $p->pic_code = (string)$v1['picCode'];
                $p->pic_file_name = $v1['picFileName'];
                $p->pic_address = $v1['picAddress'];
                if (!$p->save()){
                    throw new Exception(Model::getModelError($p));
                }
            }
            $log = new EdiviOrderSendLogs();
            $log->edivi_credit_order_id = $edivi_c->id;
            $log->b_code = $busiCode;
            $log->to_api_data = json_encode($data, JSON_UNESCAPED_UNICODE);
            $log->ed_data = json_encode($_posts, JSON_UNESCAPED_UNICODE);
            $log->to_ed_data = json_encode($posts, JSON_UNESCAPED_UNICODE);
            $log->ed_resp_code = (string)$contents['code'];
            $log->ed_resp_data = json_encode($contents, JSON_UNESCAPED_UNICODE);
            $log->created_at = time();
            if (!$log->save()){
                throw new Exception(Model::getModelError($log));
            }
            $trans->commit();
        }catch (Exception $e){
            $trans->rollBack();
            throw $e;
        }
        $out = [
            'platform_resp' => $contents,
        ];
        return $out;
    }

    /**
     * 首付控制
     * @throws
     */
    public function sfControl()
    {
        // 首付款/（首付款+贷款金额合计-附加费）必须大于等于 （新车20%、二手车30%、新能源车15%）
        //提示触发条件：小于时触发提示。
        //提示内容：首付款/（首付款+贷款金额合计-附加费）必须大于等于 （新车20%、二手车30%、新能源车15%）
        //业务含义：首付款必须大于等于裸车价的（新车20%、二手车30%、新能源车15%）
        $loan_money = $this->edivi_divi->loan_money;
        $sf_money = $this->edivi_divi->sf_money;
        $finance_charge = $this->edivi_divi->finance_charge;
//        Tools::log($finance_charge);
        $x = $loan_money+$sf_money-$finance_charge;
        $y = $sf_money/$x;
        $edivi_o = $this->edivi_c_o;
        // 0：新车，1：二手车，2：新能源车
        switch ($edivi_o->car_type){
            case 0:
                if ($y < 0.2){
//                    throw new ApiException(201903050950, "首付款/（首付款+贷款金额合计-附加费）必须大于等于新车20%");
                }
                break;
            case 1:
                if ($y < 0.3){
//                    throw new ApiException(201903050950, "首付款/（首付款+贷款金额合计-附加费）必须大于等于二手车30%");
                }
                break;
            case 2:
                if ($y < 0.15){
//                    throw new ApiException(201903050950, "首付款/（首付款+贷款金额合计-附加费）必须大于等于新能源车15%");
                }
                break;
            default:
                throw new ApiException(201903050940, "没有找到车辆类型");
                break;
        }
    }

    /**
     * 附加费控制
     * @throws
     */
    public function fjfControl()
    {
        // （1）附加费不能大于贷款金额。
        //（2）新车、新能源附加费不超过10万，二手车附加费不超过五万。
        //（3）附加费/（首付款+贷款金额合计-附加费）必须小于等于（新车、二手车20%、新能源车15%、）
        //提示触发条件：大于时触发提示
        //提示内容：附加费/（首付款+贷款金额合计-附加费）必须小于等于裸车价（新车、二手车20%、新能源车15%、）
        $loan_money = $this->edivi_divi->loan_money;
        $sf_money = $this->edivi_divi->sf_money;
        $finance_charge = $this->edivi_divi->finance_charge;
        if ($finance_charge > $loan_money){
//            throw new ApiException(201903050959, "附加费不能大于贷款金额");
        }
        $x = $loan_money+$sf_money-$finance_charge;
        $y = $finance_charge/$x;
        $edivi_o = $this->edivi_c_o;
        // 0：新车，1：二手车，2：新能源车
        switch ($edivi_o->car_type){
            case 0:
                if ($finance_charge > 100000){
//                    throw new ApiException(201903051000, "新车附加费不超过10万");
                }
                if ($y > 0.2){
//                    throw new ApiException(201903051003, "附加费/（首付款+贷款金额合计-附加费）必须小于等于新车20%");
                }
                break;
            case 1:
                if ($finance_charge > 50000){
//                    throw new ApiException(201903051002, "二手车附加费不超过5万");
                }
                if ($y > 0.2){
//                    throw new ApiException(201903051005, "附加费/（首付款+贷款金额合计-附加费）必须小于等于二手车20%");
                }
                break;
            case 2:
                if ($finance_charge > 100000){
//                    throw new ApiException(201903051001, "新能源附加费不超过10万");
                }
                if ($y > 0.15){
//                    throw new ApiException(201903051004, "附加费/（首付款+贷款金额合计-附加费）必须小于等于新能源车15%");
                }
                break;
            default:
                throw new ApiException(201903050940, "没有找到车辆类型");
                break;
        }
    }

    /**
     * 开卡数判断
     * @param $data
     * @throws
     */
    public function cardCountCheck($data)
    {
        $card_count = 1;
        if (isset($data['financeChargeCardInfo'])){
            $a = $data['financeChargeCardInfo'];
            if (isset($a['marketId'])){
                $market_id = $a['marketId'];
                $this->edivi_divi->market_id = $market_id;
            }
            if (isset($a['financeCharge'])){
                $finance_charge = $a['financeCharge'];
                $this->edivi_divi->finance_charge = $finance_charge;
            }
            if (isset($a['guaranteeCode'])){
                $guarantee_code = $a['guaranteeCode'];
                $this->edivi_divi->guarantee_code = $guarantee_code;
                if ($market_id&&$finance_charge){
                    $card_count = 2;
                }
            }            
        }
        $edivi_o = $this->edivi_c_o;
        $card = $edivi_o->ediviOrderCard;
        $card->card_count = $card_count;
        if (!$card->save()){
            throw new Exception(Model::getModelError($card));
        }
    }

    /**
     * 贷款层数控制
     * @param $data
     * @throws
     */
    public function dkcsControl()
    {
        // 1）当开一张卡时：
        //贷款金额合计/（首付款+贷款金额合计-附加费）必须小于等于 80%（新车80%、二手车70%、新能源车85%）
        //提示触发条件：大于时触发提示
        //提示内容：贷款金额合计/（首付款+贷款金额合计-附加费）必须小于等于 80%（新车80%、二手车70%、新能源车85%）
        //业务含义：贷款总额（含附加费）必须小于等于裸车价的80%
        //（2）当开二张卡时（当附加费、附加费营销档案编号、附加费担保合同/担保承诺函编号都有值的时候，即判定位开二张卡）
        //贷款金额合计-附加费/（首付款+贷款金额合计-附加费）必须小于等于 80%（新车80%、二手车70%、新能源车85%）
        //提示触发条件：大于时触发提示
        //提示内容：贷款金额合计-附加费/（首付款+贷款金额合计-附加费）必须小于等于 80%（新车80%、二手车70%、新能源车85%）
        //业务含义：贷款本金（不含附加费）必须小于等于裸车价的（新车80%、二手车70%、新能源车85%）
        $edivi_o = $this->edivi_c_o;
        $card = $edivi_o->ediviOrderCard;
        $loan_money = $this->edivi_divi->loan_money;
        $sf_money = $this->edivi_divi->sf_money;
        $finance_charge = $this->edivi_divi->finance_charge;
        switch ($card->card_count){
            case 1:
                $x = $loan_money+$sf_money-$finance_charge;
                $y = $loan_money/$x;
                $label = "贷款金额合计";
                break;
            case 2:
                $x = $loan_money+$sf_money-$finance_charge;
                $x1 = $loan_money-$finance_charge;
                $y = $loan_money/$x;
                $label = "贷款本金（不含附加费）";
                break;
            default:
                throw new ApiException(201904051043, "开卡数必须为1或2");
                break;
        }
        switch ($edivi_o->car_type){
            case 0:
                if ($y > 0.8){
//                    throw new ApiException(201904051044, "{$label}/（首付款+贷款金额合计-附加费）必须小于等于新车80%");
                }
                break;
            case 1:
                if ($y > 0.7){
//                    throw new ApiException(201904051045, "{$label}/（首付款+贷款金额合计-附加费）必须小于等于二手车70%");
                }
                break;
            case 2:
                if ($y > 0.85){
//                    throw new ApiException(201904051046, "{$label}/（首付款+贷款金额合计-附加费）必须小于等于新能源85%");
                }
                break;
            default:
                throw new ApiException(201904051047, "没有找到车辆类型");
                break;
        }
    }

    /**
     * 抵押率判断
     * @param $data
     * @throws
     */
    public function dylCheck($data)
    {
        // 最高抵押率，不能超过，新车80%、二手车70%、新能源85%。
        //不满足提示：“最高抵押率，不能超过，新车80%、二手车70%、新能源85%
        if (isset($data['guaranteeInfo']['maxmortrate'])){
            $this->edivi_divi->maxmortrate = $data['guaranteeInfo']['maxmortrate'];
        }
    }

    /**
     * 贷款金额合计控制
     * @throws
     */
    public function dkjehjControl($data)
    {
        //（1）一张卡时：（抵押品价值*最高抵押率+保证总金额）大于等于贷款金额合计，未传值字段按0，加入计算。 业务模式为：质押时，不执行该判断。
        //       二张卡时：（抵押品价值*最高抵押率+保证总金额）大于等于（贷款金额合计-附加费），未传值字段按0，加入计算。 业务模式为：质押时，不执行该判断。​
        //（2）当支付模式为：受托支付时，第三方收款金额1+第三方收款金额2+合作单位收款金额（数组）必须等于贷款金额合计（ loanMoney），未传字段视为0加入计算。
        if (isset($data['guaranteeInfo']['mortvalue'])){
            $this->edivi_divi->mortvalue = $data['guaranteeInfo']['mortvalue'];
        }
        if (isset($data['guaranteeInfo']['bailamount'])){
            $this->edivi_divi->bailamount = $data['guaranteeInfo']['bailamount'];
        }
        $x = $this->edivi_divi->mortvalue * $this->edivi_divi->maxmortrate + $this->edivi_divi->bailamount;
        switch ($this->edivi_c_o->ediviOrderCard->card_count){
            case 1:
                $y = $this->edivi_divi->loan_money;
                $label = "贷款金额合计";
                break;
            case 2:
                $y = $this->edivi_divi->loan_money - $this->edivi_divi->finance_charge;
                $label = "（贷款金额合计-附加费）";
                break;
        }
        if ($x < $y){
//            throw new ApiException(201903051132, "（抵押品价值*最高抵押率+保证总金额）大于等于{$label}");
        }
    }

    /**
     * 车损险投保金额控制
     * @throws
     */
    public function csxtbjeControl()
    {
        //当一张开时：控制大于等于贷款金额合计+首付款金额。若不满足，提交提示：车损险投保金额需大于等于贷款金额合计+首付款金额。
        //当两张卡时：控制大约等于贷款金额合计+首付款金额-附加费。若不满足，提交提示：车损险投保金额需大于等于贷款金额合计+首付款金额-附加费。
        $x = $this->edivi_divi->loan_money + $this->edivi_divi->sf_money - $this->edivi_divi->finance_charge;
        $y = $this->edivi_divi->insureamt;
        switch ($this->edivi_c_o->ediviOrderCard->card_count){
            case 1:
                $label = "贷款金额合计+首付款金额";
                break;
            case 2:
                $label = "贷款金额合计+首付款金额-附加费";
                break;
        }
        /*if ($y < $x) {
            throw new ApiException(201903051139, "车损险投保金额需大于等于{$label}");
        }*/
    }

    /**
     * @param int $code
     * @param int $data_code
     * @param string $msg
     * @param string $data_msg
     */
    public static function notifyBack($code = 0, $data_code = 1, $msg = "success", $data_msg = "success")
    {
        return self::generateNotifyBackJson($code, $data_code, $msg, $data_msg, "array");
//        echo self::generateNotifyBackJson($code, $data_code, $msg, $data_msg);exit;
    }

    /**
     * @param int $code
     * @param int $data_code
     * @param string $msg
     * @param string $data_msg
     * @return false|string
     */
    public static function generateNotifyBackJson($code = 0, $data_code = 1, $msg = "success", $data_msg = "success", $r_type = 'json')
    {
        $r = [
            'code' => $code,
            'msg' => $msg,
            'data' => [
                'code' => $data_code,
                'msg' => $data_msg,
            ],
        ];
        if ($r_type == "json"){
            return json_encode($r, JSON_UNESCAPED_UNICODE);
        }
        if ($r_type == "array"){
            return $r;
        }
    }

    const B_CODE_CREDIT = '1001';
    const B_CODE_DIVI_INFO_UP = '1002';
    const B_CODE_CARD_INFO_UP = '1003';
    const B_CODE_ORDER_NOTIFY = '1004';
    const B_CODE_SUPPLEMENTARY_STAGING_MATERIALS = '1005';
    const B_CODE_SUPPLEMENT_OF_MORTGAGE_INFORMATION = '1006';
    const B_CODE_CREDIT_CONFIRM = '1008';
    const B_CODE_HT_DOWNLOAD = '1009';
    const B_CODE_ORDER_INFO = '1011';
    const B_CODE_ORDER_APPLY = '1012';
    const B_CODE_LAST_NOTIFY_CHECK = '1013';
    const B_CODE_COMPENSATION_ASSOCIATES = '1015';
    public static function getBCodes()
    {
        // 业务编码
        return [
            self::B_CODE_CREDIT => "征信查询接口",
            self::B_CODE_DIVI_INFO_UP => "分期信息上送接口",
            self::B_CODE_CARD_INFO_UP => "开卡信息上送接口",
            self::B_CODE_ORDER_NOTIFY => "订单通知",
            self::B_CODE_SUPPLEMENTARY_STAGING_MATERIALS => "补充分期材料",
            self::B_CODE_SUPPLEMENT_OF_MORTGAGE_INFORMATION => "抵押信息补录",
            self::B_CODE_CREDIT_CONFIRM => "信息确认",
            self::B_CODE_HT_DOWNLOAD => "电子签约合同与证据链下载",
            self::B_CODE_ORDER_INFO => "订单状态",
            self::B_CODE_ORDER_APPLY => "订单申请",
            self::B_CODE_LAST_NOTIFY_CHECK => "订单的最新通知记录查询",
            self::B_CODE_COMPENSATION_ASSOCIATES => "补查关联人",
        ];
    }

    /**
     * 获取国籍
     * @return array
     */
    public static function getNationality()
    {
        return [
            '4' => "阿富汗",
            '8' => "阿尔巴尼亚",
            '10' => "南极洲",
            '12' => "阿尔及利亚",
            '16' => "美属萨摩亚",
            '20' => "安道尔",
            '24' => "安哥拉",
            '28' => "安提瓜和巴布达",
            '31' => "阿塞拜疆",
            '32' => "阿根廷",
            '36' => "澳大利亚",
            '40' => "奥地利",
            '44' => "巴哈马",
            '48' => "巴林",
            '50' => "孟加拉国",
            '51' => "亚美尼亚",
            '52' => "巴巴多斯",
            '56' => "比利时",
            '60' => "百慕大",
            '64' => "不丹",
            '68' => "玻利维亚",
            '70' => "波黑",
            '72' => "博茨瓦纳",
            '74' => "布维岛",
            '76' => "巴西",
            '84' => "伯利兹",
            '86' => "英属印度洋领地",
            '90' => "所罗门群岛",
            '92' => "英属维尔京群岛",
            '96' => "文莱",
            '100' => "保加利亚",
            '104' => "缅甸",
            '108' => "布隆迪",
            '112' => "白俄罗斯",
            '116' => "柬埔寨",
            '120' => "喀麦隆",
            '124' => "加拿大",
            '132' => "佛得角",
            '136' => "开曼群岛",
            '140' => "中非",
            '144' => "斯里兰卡",
            '148' => "乍得",
            '152' => "智利",
            '156' => "中国",
            '158' => "中国台湾",
            '162' => "圣诞岛",
            '166' => "科科斯（基林）群岛",
            '170' => "哥伦比亚",
            '174' => "科摩罗",
            '175' => "马约特",
            '178' => "刚果（布）",
            '180' => "刚果（金）",
            '184' => "库克群岛",
            '188' => "哥斯达黎加",
            '191' => "克罗地亚",
            '192' => "古巴",
            '196' => "塞浦路斯",
            '203' => "捷克",
            '204' => "贝宁",
            '208' => "丹麦",
            '212' => "多米尼克",
            '214' => "多米尼加",
            '218' => "厄瓜多尔",
            '222' => "萨尔瓦多",
            '226' => "赤道几内亚",
            '231' => "埃塞俄比亚",
            '232' => "厄立特里亚",
            '233' => "爱沙尼亚",
            '234' => "法罗群岛",
            '238' => "福克兰群岛（马尔维纳斯）",
            '239' => "南乔治亚岛和南桑德韦奇岛",
            '242' => "斐济",
            '246' => "芬兰",
            '250' => "法国",
            '254' => "法属圭亚那",
            '258' => "法属波利尼西亚",
            '260' => "法属南部领地",
            '262' => "吉布提",
            '266' => "加蓬",
            '268' => "格鲁吉亚",
            '270' => "冈比亚",
            '275' => "巴勒斯坦",
            '276' => "德国",
            '288' => "加纳",
            '292' => "直布罗陀",
            '296' => "基里巴斯",
            '300' => "希腊",
            '304' => "格陵兰",
            '308' => "格林纳达",
            '312' => "瓜德罗普",
            '316' => "关岛",
            '320' => "危地马拉",
            '324' => "几内亚",
            '328' => "圭亚那",
            '332' => "海地",
            '334' => "赫德岛和麦克唐纳岛",
            '336' => "梵蒂冈",
            '340' => "洪都拉斯",
            '344' => "中国香港",
            '348' => "匈牙利",
            '352' => "冰岛",
            '356' => "印度",
            '360' => "印度尼西亚",
            '364' => "伊朗",
            '368' => "伊拉克",
            '372' => "爱尔兰",
            '376' => "以色列",
            '380' => "意大利",
            '384' => "科特迪瓦",
            '388' => "牙买加",
            '392' => "日本",
            '398' => "哈萨克斯坦",
            '400' => "约旦",
            '404' => "肯尼亚",
            '408' => "朝鲜",
            '410' => "韩国",
            '414' => "科威特",
            '417' => "吉尔吉斯斯坦",
            '418' => "老挝",
            '422' => "黎巴嫩",
            '426' => "莱索托",
            '428' => "拉脱维亚",
            '430' => "利比里亚",
            '434' => "利比亚",
            '438' => "列支敦士登",
            '440' => "立陶宛",
            '442' => "卢森堡",
            '446' => "中国澳门",
            '450' => "马达加斯加",
            '454' => "马拉维",
            '458' => "马来西亚",
            '462' => "马尔代夫",
            '466' => "马里",
            '470' => "马耳他",
            '474' => "马提尼克",
            '478' => "毛里塔尼亚",
            '480' => "毛里求斯",
            '484' => "墨西哥",
            '492' => "摩纳哥",
            '496' => "蒙古",
            '498' => "摩尔多瓦",
            '500' => "蒙特塞拉特",
            '504' => "摩洛哥",
            '508' => "莫桑比亚",
            '512' => "阿曼",
            '516' => "纳米比亚",
            '520' => "瑙鲁",
            '524' => "尼泊尔",
            '528' => "荷兰",
            '530' => "荷属安的列斯",
            '533' => "阿鲁巴",
            '540' => "新喀里多尼亚",
            '548' => "瓦努阿图",
            '554' => "新西兰",
            '558' => "尼加拉瓜",
            '562' => "尼日尔",
            '566' => "尼日利亚",
            '570' => "纽埃",
            '574' => "诺福克岛",
            '578' => "挪威",
            '580' => "北马里亚纳",
            '581' => "美国本土外小岛屿",
            '583' => "密克罗尼西亚联邦",
            '584' => "马绍尔群岛",
            '585' => "帕劳",
            '586' => "巴基斯坦",
            '591' => "巴拿马",
            '598' => "巴布亚新几内亚",
            '600' => "巴拉圭",
            '604' => "秘鲁",
            '608' => "菲律宾",
            '612' => "皮特凯恩",
            '616' => "波兰",
            '620' => "葡萄牙",
            '624' => "几内亚比绍",
            '626' => "东帝汶",
            '630' => "波多黎各",
            '634' => "卡塔尔",
            '638' => "留尼汪",
            '642' => "罗马尼亚",
            '643' => "俄罗斯联邦",
            '646' => "卢旺达",
            '654' => "圣赫勒拿",
            '659' => "圣基茨和尼维斯",
            '660' => "安圭拉",
            '662' => "圣卢西亚",
            '666' => "圣皮埃尔和密克隆",
            '670' => "圣文森特和格林纳丁斯",
            '674' => "圣马力诺",
            '678' => "圣多美和普林西比",
            '682' => "沙特阿拉伯",
            '686' => "塞内加尔",
            '690' => "塞舌尔",
            '694' => "塞拉利昂",
            '702' => "新加坡",
            '703' => "斯洛伐克",
            '704' => "越南",
            '705' => "斯洛文尼亚",
            '706' => "索马里",
            '710' => "南非",
            '716' => "津巴布韦",
            '724' => "西班牙",
            '732' => "西撒哈拉",
            '736' => "苏丹",
            '740' => "苏里南",
            '744' => "斯瓦尔巴岛和扬马延岛",
            '748' => "斯威士兰",
            '752' => "瑞典",
            '756' => "瑞士",
            '760' => "叙利亚",
            '762' => "塔吉克斯坦",
            '764' => "泰国",
            '768' => "多哥",
            '772' => "托克劳",
            '776' => "汤加",
            '780' => "特立尼达和多巴哥",
            '784' => "阿联酋",
            '788' => "突尼斯",
            '792' => "土耳其",
            '795' => "土库曼斯坦",
            '796' => "特克斯和凯科斯群岛",
            '798' => "图瓦卢",
            '800' => "乌干达",
            '804' => "乌克兰",
            '807' => "马其顿",
            '818' => "埃及",
            '826' => "英国",
            '834' => "坦桑尼亚",
            '840' => "美国",
            '850' => "美属维尔京群岛",
            '854' => "布基纳法索",
            '858' => "乌拉圭",
            '860' => "乌兹别克斯坦",
            '862' => "委内瑞拉",
            '876' => "瓦利斯和富图纳",
            '882' => "萨摩亚",
            '883' => "塞尔维亚",
            '887' => "也门",
            '891' => "南斯拉夫",
            '894' => "赞比亚",
            '895' => "中途岛",
            '999' => "其它",
        ];
    }

    /**
     * 材料编码
     */
    public static function getMaterialCoding()
    {
        // 编码 名称 限制材料张数 材料显示方式(0:图片 1:视频)
        return [
            ['code' => "hyzkzm", 'name' => "婚姻状况证明", 'limit' => "20", 'show_type' => "0",],
            ['code' => "srzm", 'name' => "收入证明", 'limit' => "20", 'show_type' => "0",],
            ['code' => "yhls", 'name' => "银行卡流水", 'limit' => "100", 'show_type' => "0",],
            ['code' => "fcz", 'name' => "房产证", 'limit' => "100", 'show_type' => "0",],
            ['code' => "jsz", 'name' => "驾驶能力证明", 'limit' => "20", 'show_type' => "0",],
            ['code' => "qyzp", 'name' => "签约照片", 'limit' => "20", 'show_type' => "0",],
            ['code' => "gcfp", 'name' => "购车发票", 'limit' => "1", 'show_type' => "0",],
            ['code' => "hgz", 'name' => "合格证", 'limit' => "1", 'show_type' => "0",],
            ['code' => "cldjzs", 'name' => "车辆登记证书", 'limit' => "5", 'show_type' => "0",],
            ['code' => "jfzp", 'name' => "家访照片", 'limit' => "20", 'show_type' => "0",],
            ['code' => "gchtzm", 'name' => "购车合同证明", 'limit' => "20", 'show_type' => "0",],
            ['code' => "jfsp", 'name' => "家访视频", 'limit' => "5", 'show_type' => "1",],
            ['code' => "qysp", 'name' => "签约视频", 'limit' => "5", 'show_type' => "1",],
            ['code' => "sm", 'name' => "声明", 'limit' => "20", 'show_type' => "0",],
            ['code' => "clbxd", 'name' => "车辆保险单", 'limit' => "2", 'show_type' => "0",],
            ['code' => "cwrg", 'name' => "车位认购/租赁合同", 'limit' => "20", 'show_type' => "0",],
            ['code' => "cns", 'name' => "承诺书", 'limit' => "20", 'show_type' => "0",],
            ['code' => "deykqksm", 'name' => "补充说明材料", 'limit' => "20", 'show_type' => "0",],
            ['code' => "dlqzlzlqd", 'name' => "代领权证类资料清单", 'limit' => "20", 'show_type' => "0",],
            ['code' => "dlqzwts", 'name' => "代领权证委托书", 'limit' => "20", 'show_type' => "0",],
            ['code' => "dbcnh", 'name' => "担保承诺函", 'limit' => "20", 'show_type' => "0",],
            ['code' => "dbqrh", 'name' => "担保确认函", 'limit' => "20", 'show_type' => "0",],
            ['code' => "djzm", 'name' => "定金证明", 'limit' => "20", 'show_type' => "0",],
            ['code' => "escpgbg", 'name' => "二手车评估报告", 'limit' => "20", 'show_type' => "0",],
            ['code' => "grdjkdzfqywsqs", 'name' => "个人贷记卡定制分期业务申请书", 'limit' => "20", 'show_type' => "0",],
            ['code' => "grdkmtjl", 'name' => "个人贷款面谈记录", 'limit' => "20", 'show_type' => "0",],
            ['code' => "grdksqb", 'name' => "个人贷款申请表", 'limit' => "20", 'show_type' => "0",],
            ['code' => "grxdkhdhhsnrjlb", 'name' => "个人信贷客户电话核实内容记录表", 'limit' => "20", 'show_type' => "0",],
            ['code' => "grxdywsqzl", 'name' => "个人信贷业务申请资料", 'limit' => "20", 'show_type' => "0",],
            ['code' => "gyxyksqb", 'name' => "工银信用卡申请表", 'limit' => "20", 'show_type' => "0",],
            ['code' => "gtczrcns", 'name' => "共同偿债人承诺书", 'limit' => "20", 'show_type' => "0",],
            ['code' => "gcfqfkdlxy", 'name' => "购车分期付款代理协议", 'limit' => "20", 'show_type' => "0",],
            ['code' => "gffp", 'name' => "购房发票", 'limit' => "1", 'show_type' => "0",],
            ['code' => "gdjkzms", 'name' => "关单/进口证明书", 'limit' => "20", 'show_type' => "0",],
            ['code' => "hzjgdkpz", 'name' => "合作机构垫款凭证", 'limit' => "20", 'show_type' => "0",],
            ['code' => "jdcdydjzybasqb", 'name' => "机动车抵押登记质押备案申请表", 'limit' => "20", 'show_type' => "0",],
            ['code' => "jdcywwts", 'name' => "机动车业务委托书", 'limit' => "20", 'show_type' => "0",],
            ['code' => "jzhjzm", 'name' => "居住/户籍证明", 'limit' => "20", 'show_type' => "0",],
            ['code' => "lwhcjg", 'name' => "联网核查结果", 'limit' => "1", 'show_type' => "0",],
            ['code' => "lkqsd", 'name' => "领卡签收单", 'limit' => "20", 'show_type' => "0",],
            ['code' => "mdcdydcscspb", 'name' => "牡丹车贷易调查审查审批表", 'limit' => "20", 'show_type' => "0",],
            ['code' => "mdkfqfkht", 'name' => "牡丹卡分期付款合同", 'limit' => "20", 'show_type' => "0",],
            ['code' => "mdxykgcxffqhkht", 'name' => "牡丹信用卡购车消费分期还款合同", 'limit' => "20", 'show_type' => "0",],
            ['code' => "mdxykxffqywwtkk", 'name' => "牡丹信用卡消费分期业务委托扣款、分期授权书", 'limit' => "20", 'show_type' => "0",],
            ['code' => "qtclzm", 'name' => "其他财力证明", 'limit' => "100", 'show_type' => "0",],
            ['code' => "qczxfqfkdbht", 'name' => "汽车专项分期付款/担保合同", 'limit' => "20", 'show_type' => "0",],
            ['code' => "sfksjjpz", 'name' => "首付款收据及凭证", 'limit' => "20", 'show_type' => "0",],
            ['code' => "sqwtsfwf", 'name' => "授权委托书(服务费)", 'limit' => "20", 'show_type' => "0",],
            ['code' => "sdfsdzqrs", 'name' => "送达方式/地址确认书", 'limit' => "20", 'show_type' => "0",],
            ['code' => "tzzzpz", 'name' => "特种转账凭证", 'limit' => "1", 'show_type' => "0",],
            ['code' => "dcbg", 'name' => "调查报告", 'limit' => "20", 'show_type' => "0",],
            ['code' => "wtxys", 'name' => "委托协议书", 'limit' => "20", 'show_type' => "0",],
            ['code' => "wxts", 'name' => "温馨提示", 'limit' => "20", 'show_type' => "0",],
            ['code' => "xykgryxtjh", 'name' => "信用卡个人营销推荐函", 'limit' => "20", 'show_type' => "0",],
            ['code' => "xyksxtespb", 'name' => "信用卡授信调额审批表", 'limit' => "20", 'show_type' => "0",],
            ['code' => "ykxz", 'name' => "用卡须知", 'limit' => "20", 'show_type' => "0",],
            ['code' => "yqhkzm", 'name' => "逾期还款证明", 'limit' => "20", 'show_type' => "0",],
            ['code' => "zyzm", 'name' => "职业证明", 'limit' => "20", 'show_type' => "0",],
            ['code' => "zggsyhqczxfqspdgd", 'name' => "个人材料", 'limit' => "20", 'show_type' => "0",],
            ['code' => "bzs", 'name' => "保证书", 'limit' => "20", 'show_type' => "0",],
            ['code' => "WGDCL", 'name' => "其他贷款材料", 'limit' => "", 'show_type' => "0",],
        ];
    }

    /**
     * 接口返回说明
     * @return array
     */
    public static function getRespInfo()
    {
        return [
            '0' => "success  成功",
            '-1' => "很抱歉，系统繁忙，请稍后再试",
            '1001' => "非法参数",
            '1006' => "商户未开通业务进件权限",
            '1010' => "签名不匹配",
            '1101' => "订单不存在",
            '1102' => "订单未发起分期",
            '8006' => "业务进件流程未配置",
        ];
    }

    /**
     * 抵押品分类信息
     * @return array
     */
    public static function getMortgageClassificationInformation()
    {
        return [
            ['code' => '001', 'p_code' => "0", 'name' => "房地产",],
            ['code' => '002', 'p_code' => "0", 'name' => "交通运输设备",],
            ['code' => '001001', 'p_code' => "001", 'name' => "居住用房",],
            ['code' => '001002', 'p_code' => "001", 'name' => "办公用房",],
            ['code' => '001003', 'p_code' => "001", 'name' => "商业用房",],
            ['code' => '001004', 'p_code' => "001", 'name' => "工业用房",],
            ['code' => '001005', 'p_code' => "001", 'name' => "房屋建筑物/地上附着物",],
            ['code' => '002001', 'p_code' => "002", 'name' => "车辆",],
            ['code' => '002002', 'p_code' => "002", 'name' => "船舶",],
            ['code' => '002003', 'p_code' => "002", 'name' => "其他运输设备",],
            ['code' => '001001001', 'p_code' => "001001", 'name' => "保障房",],
            ['code' => '001001002', 'p_code' => "001001", 'name' => "普通用房",],
            ['code' => '001001003', 'p_code' => "001001", 'name' => "高档住房(含别墅)",],
            ['code' => '001001004', 'p_code' => "001001", 'name' => "其他居住用房",],
            ['code' => '001002001', 'p_code' => "001002", 'name' => "办公楼(写字楼)",],
            ['code' => '001002002', 'p_code' => "001002", 'name' => "其他办公用房",],
            ['code' => '001003001', 'p_code' => "001003", 'name' => "商铺",],
            ['code' => '001003002', 'p_code' => "001003", 'name' => "宾馆酒店",],
            ['code' => '001003003', 'p_code' => "001003", 'name' => "商场超市购物中心",],
            ['code' => '001003004', 'p_code' => "001003", 'name' => "其他商业用房",],
            ['code' => '001003005', 'p_code' => "001003", 'name' => "娱乐场所",],
            ['code' => '001003006', 'p_code' => "001003", 'name' => "商住两用房",],
            ['code' => '001004001', 'p_code' => "001004", 'name' => "工业厂房",],
            ['code' => '001004002', 'p_code' => "001004", 'name' => "其他工业用房",],
            ['code' => '001005001', 'p_code' => "001005", 'name' => "其他不属于以上类型的房地产",],
            ['code' => '002001001', 'p_code' => "002001", 'name' => "家用轿车",],
            ['code' => '002002001', 'p_code' => "002002", 'name' => "游艇",],
            ['code' => '002003001', 'p_code' => "002003", 'name' => "其他运输设备",],
        ];
    }
}